/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package movies.repositoryHibernate;

import java.util.List;
import movies.domain.Person;
import movies.repository.IPersonRepository;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Hanna_Aliakseichykava
 */
public class PersonRepository extends HibernateDaoSupport
implements IPersonRepository{

    @Override
    public Person savePerson(Person person) {

        Integer id = (Integer)getHibernateTemplate().save(person);
        person.setDay(id);
         return person;
    }

    @Override
    public Person getPersonById(int id) {
         List < Person > persons = getHibernateTemplate().
             findByNamedQuery("GetPersonById", id);

        if (persons.size() == 1) {
            return persons.get(0);
        } else {
            return null;
        }
    }

    @Override
    public void deletePerson(Person person) {
        getHibernateTemplate().delete(person);
    }

    @Override
    public List<Person> getPersonsByName(String name) {
        List < Person > persons = getHibernateTemplate().
             findByNamedQuery("GetPersonsByName", name);

        if (persons.size() > 0) {
            return persons;
        } else {
            return null;
        }
    }

    @Override
    public List<Person> getPersonsBySurname(String surname) {
        List < Person > persons = getHibernateTemplate().
             findByNamedQuery("GetPersonsBySurname", surname);

        if (persons.size() > 0) {
            return persons;
        } else {
            return null;
        }
    }

     @Override
    public List<Person> getPersonsByNameAndSurname
             (String name, String surname) {
        List < Person > persons = getHibernateTemplate().
             findByNamedQuery("GetPersonsByNameAndSurname",
             new Object[]{name, surname});

        if (persons.size() > 0) {
            return persons;
        } else {
            return null;
        }
    }

    @Override
    public List<Person> getAllPersons() {
        List < Person > persons = getHibernateTemplate().
             findByNamedQuery("GetAllPersons");

        if (persons.size() > 0) {
            return persons;
        } else {
            return null;
        }
    }


}
